package com.tjian.fafa.user.module.courier.util;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.tjian.fafa.user.R;
import com.tjian.fafa.user.module.courier.bean.MoodInfoBean;
import com.tjian.fafa.user.module.home.adapter.CourierListAdapter;
import com.tjian.fafa.user.module.home.result.HomeResult;
import com.tjian.fafa.user.module.listview.XListView;
import com.tjian.fafa.user.util.text.TextUtil;

import org.json.JSONArray;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class CourierUtil extends Activity
{
    // 刷新
    private static int LOAD_REFRESH = 0;

    // 查看更多
    private static int LOAD_MORE = 1;
    private static int page = 1;
    private static List<MoodInfoBean> allMoodInfo = new ArrayList<MoodInfoBean>();
    private static List<HomeResult> allHomeResult = new ArrayList<HomeResult>();
    static MoodInfoBean moodInfo;
    static HomeResult courierBean;
    private static BaseAdapter myListViewAdapter = null;
    private static XListView myListView;
    private static Dialog loadbar = null;

    /**
     * 获取首页数据
     *
     * @return
     */
    public static List<HomeResult> getFriendDynamicDataList(Context mContext, List<HomeResult> mMyHomeResults)
    {
        if (mMyHomeResults.isEmpty())
        {
            InputStream inputStream;
            try
            {
                inputStream = mContext.getAssets().open("data/my_home_friends.tj");
                String json = TextUtil.readTextFile(inputStream);
                String json_ankuai = null;
                JSONArray array = new JSONArray(json);
                HomeResult result = null;
                for (int i = 0; i < array.length(); i++)
                {
                    result = new HomeResult();

                    result.setUserNumber(array.getJSONObject(i).getString("userNumber"));
                    result.setName(array.getJSONObject(i).getString("name"));
                    result.setAvatar(array.getJSONObject(i).getInt("avatar"));
                    result.setWaitTaskStatus(array.getJSONObject(i).getString("waitTaskStatus"));
                    result.setAccumulative_delivery_quantity(array.getJSONObject(i).getString("accumulative_delivery_quantity"));
                    if (array.getJSONObject(i).has("average_speed_of_delivery"))
                    {
                        result.setAverage_speed_of_delivery(array.getJSONObject(i).getString("average_speed_of_delivery"));
                    }
                    if (array.getJSONObject(i).has("applauseRate"))
                    {
                        result.setApplauseRate(array.getJSONObject(i).getInt(
                                "applauseRate"));
                    }

                    if (array.getJSONObject(i).has("photo"))
                    {
                        result.setPhoto(array.getJSONObject(i).getString(
                                "photo"));
                    }
                    mMyHomeResults.add(result);
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return mMyHomeResults;
    }


    //加载数据
    public static void friendDynamicUpdateView(Context mContext, ListView mListView, List<HomeResult> mMyHomeResults)
    {
        mListView.setAdapter(new CourierListAdapter(mContext, mMyHomeResults));
    }

    /**
     * 关闭进度
     */
    private static void onLoad()
    {
        myListView.stopRefresh();
        myListView.stopLoadMore();
        myListView.setRefreshTime("刚刚");
    }

    public static void close()
    {
        if (loadbar != null)
        {
            if (loadbar.isShowing())
            {
                loadbar.dismiss();
            }
        }
    }

    public void initProgressDialog()
    {
        if (loadbar == null)
        {
            loadbar = new Dialog(this, R.style.load_dialog);
            LayoutInflater mInflater = this.getLayoutInflater();
            View v = mInflater.inflate(R.layout.anim_load, null);
            loadbar.setContentView(v);
            loadbar.setCancelable(true);
            loadbar.show();
        }
        else
        {
            loadbar.show();
        }
    }

}